{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from matplotlib.font_manager import FontProperties\n",
    "from matplotlib import font_manager\n",
    "from matplotlib import rcParams\n",
    "\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']  # 防止无法显示中文并设置黑体\n",
    "plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号\n",
    "\n",
    "TimesSong = FontProperties(fname=\"C:\\\\Users\\\\Linzy\\\\Desktop\\\\TimesSong.ttf\")\n",
    "font_manager.fontManager.addfont(\"C:\\\\Users\\\\Linzy\\\\Desktop\\\\TimesSong.ttf\")\n",
    "\n",
    "# 全局设置字体及大小，设置公式字体即可\n",
    "config = {\n",
    "    \"mathtext.fontset\": 'stix',\n",
    "    \"font.family\": 'serif',\n",
    "    \"font.serif\": ['TimesSong'],\n",
    "    'axes.unicode_minus': False,  # 处理负号，即-号\n",
    "    \"font.size\": 12\n",
    "}\n",
    "\n",
    "rcParams.update(config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fcmondf=pd.read_excel('./data/climatemonth.xlsx')\n",
    "fcmondf=fcmondf[fcmondf['年(年)'].isin(range(1991,2021,1))]\n",
    "fcmondf=fcmondf.replace(999999,np.nan)\n",
    "fcmondf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fcdaydf=pd.read_excel('./data/climateday.xlsx')\n",
    "fcdaydf=fcdaydf[fcdaydf['年(年)'].isin(range(1991,2021,1))]\n",
    "fcdaydf=fcdaydf.replace(999999,np.nan)\n",
    "fcdaydf=fcdaydf.replace(999990.0,np.nan)\n",
    "fcdaydf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 每年平均降水"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rainalllist=[]\n",
    "for y_i in range(1991,2021,1):\n",
    "    a=[]\n",
    "    for m_i in range(1,13,1):\n",
    "        temp=fcdaydf[fcdaydf['年(年)']==y_i]\n",
    "        temp=temp[temp['月(月)']==m_i]['20-20时降水量(毫米)'].sum()\n",
    "        a.append(np.round(temp,2))\n",
    "    rainalllist.append(a)\n",
    "np.array(rainalllist).mean(axis=0).round(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 每月极端降水"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "year=np.arange(1991,2021,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 极大降水和出现的年份\n",
    "maxrainmonth=[]\n",
    "maxrainvalues=[]\n",
    "for i in range(12):\n",
    "    monthlist=np.array(rainalllist)[:,i]\n",
    "    maxrainmonth.append(year[list(monthlist).index(monthlist.max())])\n",
    "    maxrainvalues.append(monthlist.max())\n",
    "maxrainmonth,maxrainvalues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 极少降水和出现的年份\n",
    "minrainmonth=[]\n",
    "minrainvalues=[]\n",
    "for i in range(12):\n",
    "    monthlist=np.array(rainalllist)[:,i]\n",
    "    minrainmonth.append(year[list(monthlist).index(monthlist.min())])\n",
    "    minrainvalues.append(monthlist.min())\n",
    "minrainmonth,minrainvalues"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 平均温度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rainalllist=[]\n",
    "for y_i in range(1991,2021,1):\n",
    "    a=[]\n",
    "    for m_i in range(1,13,1):\n",
    "        temp=fcdaydf[fcdaydf['年(年)']==y_i]\n",
    "        temp=temp[temp['月(月)']==m_i]['平均气温(摄氏度(℃))'].mean()\n",
    "        a.append(np.round(temp,2))\n",
    "    rainalllist.append(a)\n",
    "np.array(rainalllist).mean(axis=0).round(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 每月极端温度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rainalllist=[]\n",
    "for y_i in range(1991,2021,1):\n",
    "    a=[]\n",
    "    for m_i in range(1,13,1):\n",
    "        temp=fcdaydf[fcdaydf['年(年)']==y_i]\n",
    "        temp=temp[temp['月(月)']==m_i]['最高气温(摄氏度(℃))'].max()\n",
    "        a.append(np.round(temp,2))\n",
    "    rainalllist.append(a)\n",
    "np.array(rainalllist).mean(axis=0).round(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 极大降水和出现的年份\n",
    "maxTmonth=[]\n",
    "maxTvalues=[]\n",
    "for i in range(12):\n",
    "    monthlist=np.array(rainalllist)[:,i]\n",
    "    maxTmonth.append(year[list(monthlist).index(monthlist.max())])\n",
    "    maxTvalues.append(monthlist.max())\n",
    "maxTmonth,maxTvalues"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rainalllist=[]\n",
    "for y_i in range(1991,2021,1):\n",
    "    a=[]\n",
    "    for m_i in range(1,13,1):\n",
    "        temp=fcdaydf[fcdaydf['年(年)']==y_i]\n",
    "        temp=temp[temp['月(月)']==m_i]['最低气温(摄氏度(℃))'].min()\n",
    "        a.append(np.round(temp,2))\n",
    "    rainalllist.append(a)\n",
    "np.array(rainalllist).mean(axis=0).round(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 极小温度和出现的年份\n",
    "minTmonth=[]\n",
    "minTvalues=[]\n",
    "for i in range(12):\n",
    "    monthlist=np.array(rainalllist)[:,i]\n",
    "    minTmonth.append(year[list(monthlist).index(monthlist.min())])\n",
    "    minTvalues.append(monthlist.min())\n",
    "minTmonth,minTvalues"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 绘图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 降水"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "colname=['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "rolname=['平均雨量','月极大值','月极小值']\n",
    "cellvalue=[Y_mean,Y_max,Y_min]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig=plt.figure(figsize=(16,9))\n",
    "ax=fig.subplots(1,1)\n",
    "wid=0.2\n",
    "ax.bar(x-wid,Y_mean,width=wid,color='#00fafa',edgecolor='k',lw=2,label='平均雨量',zorder=10)\n",
    "ax.bar(x,Y_max,width=wid,color='#3366ff',edgecolor='k',lw=2,label='月极大值',zorder=10)\n",
    "ax.bar(x+wid,Y_min,width=wid,color='#b22829',edgecolor='k',lw=2,label='月极小值',zorder=10)\n",
    "ax.set_xticks(range(1,13,1))\n",
    "ax.set_xlim(0.5,12.55)\n",
    "ax.table(cellText=cellvalue,\n",
    "        #  colWidths=[0.08]*12,\n",
    "         colLabels=colname,\n",
    "         rowLabels=rolname,\n",
    "         rowLoc='center',\n",
    "         cellLoc='center',\n",
    "         rowColours='y'*3,\n",
    "         colColours='y'*12,\n",
    "         loc='top')\n",
    "\n",
    "for i in range(1,13,1):\n",
    "    ax.text(i,Y_max[i-1]+10,str(Y_maxyear[i-1]),horizontalalignment='center',size=15)\n",
    "    ax.text(i+0.55,Y_min[i-1]+10,str(Y_minyear[i-1]),horizontalalignment='right',size=15)\n",
    "ax.set_ylabel('降水量（mm）',size=20)\n",
    "# ax.set_title('降水量（mm）',size=20)\n",
    "ax.set_xticklabels(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'])\n",
    "ax.tick_params(labelsize=20)\n",
    "fig.legend(loc = 'lower center',ncol=3,frameon = False,bbox_to_anchor=(0.5, -0.02),prop = {'size':25})\n",
    "ax.grid(axis='y',zorder=0,)\n",
    "plt.savefig('./outputFC/rainfc59061.png',dpi=300,bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 温度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "rolname=['平均气温','月极高值','月极低值']\n",
    "cellvalue=[Y_mean,Y_max,Y_min]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig=plt.figure(figsize=(16,9))\n",
    "ax=fig.subplots(1,1)\n",
    "wid=0.2\n",
    "ax.bar(x-wid,Y_mean,width=wid,color='#0000ff',edgecolor='k',lw=2,label='平均气温',zorder=10)\n",
    "ax.bar(x,Y_max,width=wid,color='r',edgecolor='k',lw=2,label='月最高值',zorder=10)\n",
    "ax.bar(x+wid,Y_min,width=wid,color='#00ffd2',edgecolor='k',lw=2,label='月最低值',zorder=10)\n",
    "ax.set_xticks(range(1,13,1))\n",
    "ax.set_xlim(0.5,12.55)\n",
    "ax.set_ylim(-6.1,46)\n",
    "ax.set_yticks(range(-5,46,5))\n",
    "\n",
    "ax.table(cellText=cellvalue,\n",
    "        #  colWidths=[0.08]*12,\n",
    "         colLabels=colname,\n",
    "         rowLabels=rolname,\n",
    "         rowLoc='center',\n",
    "         cellLoc='center',\n",
    "         rowColours='y'*3,\n",
    "         colColours='y'*12,\n",
    "         loc='top')\n",
    "\n",
    "ax.axhline(0, linestyle='-', color='k',lw=2) # 水平线，y需要传入一个具体数值，比如上图是0\n",
    "for i in range(1,13,1):\n",
    "    ax.text(i,Y_max[i-1]+1,str(Y_maxyear[i-1]),horizontalalignment='center',size=15)\n",
    "    if i in [1,2,12]:\n",
    "        ax.text(i+0.2,Y_min[i-1]-1.3,str(Y_minyear[i-1]),horizontalalignment='center',size=15)\n",
    "    else:\n",
    "        ax.text(i+0.55,Y_min[i-1]+1,str(Y_minyear[i-1]),horizontalalignment='right',size=15)\n",
    "ax.set_ylabel('温度（℃）',size=20)\n",
    "# ax.set_title('温度（℃）',size=20)\n",
    "ax.set_xticklabels(['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'])\n",
    "ax.tick_params(labelsize=20)\n",
    "fig.legend(loc = 'lower center',ncol=3,frameon = False,bbox_to_anchor=(0.5, -0.02),prop = {'size':25})\n",
    "ax.grid(axis='y',zorder=0,)\n",
    "plt.savefig('./outputFC/Tfc59061.png',dpi=300,bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py310",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
